//
//  TagImgeViewController.m
//  帮惠管理员
//
//  Created by reew on 2017/10/19.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "TagImgeViewController.h"
#import <QBImagePickerController/QBImagePickerController.h>
#import "CommonUtils.h"
#import "System.h"
#import "Admin.h"
#import "DetailImageViewController.h"
@interface TagImgeViewController () <QBImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *firstImage;
@property (weak, nonatomic) IBOutlet UIImageView *secondImage;
@property (weak, nonatomic) IBOutlet UIImageView *thirdImage;

@property (weak, nonatomic) IBOutlet UITextField *firstText_one;
@property (weak, nonatomic) IBOutlet UITextField *secondTex_one;

@property (weak, nonatomic) IBOutlet UITextField *firstText_two;
@property (weak, nonatomic) IBOutlet UITextField *secondText_two;

@property (weak, nonatomic) IBOutlet UITextField *firstText_three;
@property (weak, nonatomic) IBOutlet UITextField *secondText_three;

@property (nonatomic,strong) QBImagePickerController *imagePicker;


@property (nonatomic) NSUInteger index;

@property (nonatomic,strong) UIImage *image1;
@property (nonatomic,strong) UIImage *image2;
@property (nonatomic,strong) UIImage *image3;
@end

@implementation TagImgeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    
    //初始化照片选择器
    [self initPickerView];
    //添加手势
    [self addGesture];
    
}
- (void)addGesture{
    _firstImage.userInteractionEnabled = YES;
    _secondImage.userInteractionEnabled = YES;
    _thirdImage.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openAlbulm:)];
    [self.firstImage addGestureRecognizer:tapGestureRecognizer1];
    
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openAlbulm:)];
    [self.secondImage addGestureRecognizer:tapGestureRecognizer2];
    
    UITapGestureRecognizer *tapGestureRecognizer3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openAlbulm:)];
    [self.thirdImage addGestureRecognizer:tapGestureRecognizer3];
}
- (void)openAlbulm:(UITapGestureRecognizer *)gesture{
    if (gesture.view == _firstImage) {
        _index = 1;
    }else if (gesture.view == _secondImage){
        _index = 2;
    }else if (gesture.view == _thirdImage){
        _index = 3;
    }
    [self presentViewController:_imagePicker animated:YES completion:nil];
}
- (void)initPickerView{
    _imagePicker = [QBImagePickerController new];
    _imagePicker.delegate = self;
    _imagePicker.allowsMultipleSelection = NO;
//    _imagePicker.maximumNumberOfSelection = 7;
    _imagePicker.prompt = @"选择图片";
}
- (void)setupNav{
    UIBarButtonItem *rightItem= [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    //    rightItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.title = @"上传标签和图片";
}
- (void)save{
    if (_firstText_one.text.length == 0) {
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if (_secondTex_one.text.length == 0){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if (_firstText_two.text.length == 0){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if (_secondText_two.text.length == 0){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if (_firstText_three.text.length == 0){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if (_secondText_three.text.length == 0){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if(_image1 == nil){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if (_image2 == nil){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else if(_image3 == nil){
        [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@""];
    }else{
        NSString *url = [kBaseURL stringByAppendingPathComponent:kTag_img];
        [CommonUtils sendPOST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            UIImage *image = [UIImage imageNamed:@"chapter8_3_1"];
//            第一张图片
            NSString *firstImageName = [NSString stringWithFormat:@"%@-%@.png",_firstText_one.text,_secondTex_one.text];
            [formData appendPartWithFileData:UIImageJPEGRepresentation(_image1, 0.1) name:@"file" fileName:firstImageName mimeType:@"image/png"];
//            第二张图片
            NSString *secondImageName = [NSString stringWithFormat:@"%@-%@.png",_firstText_two.text,_secondText_two.text];
            [formData appendPartWithFileData:UIImageJPEGRepresentation(_image2, 0.1) name:@"file" fileName:secondImageName mimeType:@"image/png"];
//            第三张图片
            NSString *thirdImageName = [NSString stringWithFormat:@"%@-%@.png",_firstText_three.text,_secondText_three.text];
            [formData appendPartWithFileData:UIImageJPEGRepresentation(_image3, 0.1) name:@"file" fileName:thirdImageName mimeType:@"image/png"];
//            GoodsNo
            [formData appendPartWithFormData:[_goodsNo dataUsingEncoding:NSUTF8StringEncoding] name:@"goodsNo"];
        } progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"%@",responseObject);
            NSString *result = responseObject[@"result"];
            NSString *message = responseObject[@"message"];
            if ([result integerValue]== 1) {
                [CommonUtils showHudWithStatus:SuccessStatus stayTime:1 info:message];
            }else{
                [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:message];
            }
            //跳转上传商品详情图页面
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            DetailImageViewController *vc = [sb instantiateViewControllerWithIdentifier:@"detailImgVc"];
            vc.goodsNo = _goodsNo;
            [self.navigationController pushViewController:vc animated:YES];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"%@",error);
            [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:@"哎呀 网络不见了"];
        }];
    }
   
}

#pragma mark - QBImagePickerController
-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets{
    for (PHAsset *set in assets) {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        
        [[PHImageManager defaultManager] requestImageForAsset:set targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
            //设置图片
            switch (_index) {
                case 1:
                    _image1 =result;
                    _firstImage.image = result;
                    break;
                case 2:
                    _image2=result;
                    _secondImage.image = result;
                    break;
                case 3:
                    _image3 =result;
                    _thirdImage.image = result;
                    break;
                default:
                    break;
            }
        }];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
-(void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
    _index = 0;
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
